﻿using ELab.Client;
using ELab.Communication;
using ELab.Controls;
using ELab.Settings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ELab.Student
{
    public partial class frmLogin : Form
    {
        public bool IsSuccess { get; private set; }
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                var serverInfor = ELabSettings.Instance.Servers.FirstOrDefault();
                string ipAddressServer = serverInfor == null ? CommunicationConsts.DefaultIpAddress : serverInfor.IpAddress;
                int portServer = serverInfor == null ? CommunicationConsts.DefaultPort : serverInfor.Port;
                var controller = new Management.ELabController(ipAddressServer, portServer);
                this.uLoginControl.Controller = controller;
                this.uLoginControl.OnCancelLogin += uLoginControl_OnCancelLogin;
                this.uLoginControl.OnLoginAnotherMachine += uLoginControl_OnLoginAnotherMachine;
                this.uLoginControl.OnLoginSuccess += uLoginControl_OnLoginSuccess;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void uLoginControl_OnLoginAnotherMachine(object sender, EventArgs e)
        {
            try
            {
                if (uLoginControl.CurrentUser.Surveys == null || uLoginControl.CurrentUser.Surveys.Length == 0)
                {
                    IsSuccess = true;
                    this.Close();
                    return;
                }
                //Do add service check
                this.Controls.Clear();
                SurveyControl uctSurvey = new SurveyControl();
                uctSurvey.AnswerSurveySuccess += uctSurvey_AnswerSurveySuccess;
                this.Controls.Add(uctSurvey);
                uctSurvey.ShowSurvey(uLoginControl.CurrentUser.Surveys);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void uctSurvey_AnswerSurveySuccess(object sender, EventArgs e)
        {
            IsSuccess = true;
            this.Close();
        }

        void uLoginControl_OnLoginSuccess(object sender, EventArgs e)
        {
            try
            {
                IsSuccess = true;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void uLoginControl_OnCancelLogin(object sender, EventArgs e)
        {
            try
            {
                IsSuccess = false;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
